<template>
  <TablePanel title="线索管理">
    <div slot="btm">
          <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              @click="handleAdd"
              v-hasPermi="['crm:leads:add']"
          >新增</el-button>
          <el-button
              type="success"
              plain
              icon="el-icon-edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['crm:leads:edit']"
          >修改</el-button>
          <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['crm:leads:remove']"
          >删除</el-button>
          <el-button
              type="warning"
              plain
              icon="el-icon-download"
              @click="handleExport"
              v-hasPermi="['crm:leads:export']"
          >导出</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <div slot="search">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="联系人：" prop="contacts">
          <el-input
            v-model="queryParams.contacts"
            placeholder="请输入联系人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="电话：" prop="telephone">
          <el-input
            v-model="queryParams.telephone"
            placeholder="请输入电话"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div slot="content">
    <el-table v-loading="loading" border :height="tableHeight" :data="leadsList" @selection-change="handleSelectionChange">
      <div slot="empty">
        <el-empty :image="noData" image-size="200">
        </el-empty>
      </div>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="customer" />
      <el-table-column label="客户性质" align="center" prop="nature" />
      <el-table-column label="客户来源" align="center" prop="source" />
      <el-table-column label="客户行业" align="center" prop="industry" />
      <el-table-column label="客户分级" align="center" prop="grade" />
      <el-table-column label="下次联系时间" align="center" prop="nextTime" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="负责人" align="center" prop="ownerUserId" />
      <el-table-column label="创建时间" align="center" prop="createdTime" />
      <el-table-column label="更新人" align="center" prop="updatedBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:leads:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:leads:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改线索对话框 -->
    <st-dialog :title="title" v-if="open" @close="cancel"
               @save="submitForm"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="客户名称：" prop="customer">
          <el-input v-model="form.customer" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户性质：" prop="nature">
          <el-input v-model="form.nature" placeholder="请输入客户性质" />
        </el-form-item>
        <el-form-item label="客户来源：" prop="source">
          <el-input v-model="form.source" placeholder="请输入客户来源" />
        </el-form-item>
        <el-form-item label="客户行业：" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入客户行业" />
        </el-form-item>
        <el-form-item label="客户分级：" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入客户分级" />
        </el-form-item>
        <el-form-item label="客户官网：" prop="website">
          <el-input v-model="form.website" placeholder="请输入客户官网" />
        </el-form-item>
        <el-form-item label="客户简介：" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="联系人：" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="邮箱：" prop="mailbox">
          <el-input v-model="form.mailbox" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="下次联系时间：" prop="nextTime">
          <el-input v-model="form.nextTime" placeholder="请输入下次联系时间" />
        </el-form-item>
        <el-form-item label="电话：" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="地址：" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
    </st-dialog>
  </div>
    <div slot="footer">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </TablePanel>
</template>

<script>
import { listLeads, getLeads, delLeads, addLeads, updateLeads } from "@/api/crm/leads";
import noData from '@/assets/img/no_data.png'
export default {
  name: "Leads",
  data() {
    return {
      noData:noData,
      tableHeight:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线索表格数据
      leadsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contacts: null,
        telephone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询线索列表 */
    getList() {
      this.loading = true;
      listLeads(this.queryParams).then(response => {
        this.leadsList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(()=>{
          this.tableHeight=this.getTableHeightWithTitle();
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        leadsId: null,
        customer: null,
        nature: null,
        source: null,
        industry: null,
        grade: null,
        website: null,
        introduction: null,
        provinceId: null,
        cityId: null,
        areaId: null,
        contacts: null,
        sex: null,
        mailbox: null,
        isTransform: null,
        followup: null,
        nextTime: null,
        telephone: null,
        address: null,
        remark: null,
        createdBy: null,
        ownerUserId: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.leadsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线索";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const leadsId = row.leadsId || this.ids
      getLeads(leadsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改线索";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.leadsId != null) {
            updateLeads(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeads(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const leadsIds = row.leadsId || this.ids;
      this.$modal.confirm('是否确认删除线索编号为"' + leadsIds + '"的数据项？').then(function() {
        return delLeads(leadsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/leads/export', {
        ...this.queryParams
      }, `leads_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
